//引入express
var express = require('express');
//引入路由
var apps = express.Router();
var UUID = require('uuid');
let db = require('../utils/db');
// 订单查询
apps.get('/order', function (req, res) {
    // console.log(req.query.size, req.query.page)
    let arr=[]
    let arr2
    let ordersql = 'SELECT *,c.name store_name,a.id order_id FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 LIMIT ' + (req.query.page - 1) * req.query.size + ',' + req.query.page * req.query.size
    let ordersql2 = 'SELECT COUNT(*) count FROM orderr where deleted=0'
    // let ordersql='SELECT *,a.num w_order_goods_center_num,a.id w_order_goods_center_id,b.id w_orderr_id,e.name w_store_name  FROM order_goods_center a LEFT JOIN orderr b ON a.order_id=b.id LEFT JOIN goods c ON a.goods_id=c.id LEFT JOIN USER d ON b.user_id=d.id LEFT JOIN store e ON b.store_id=e.id where b.deleted=0 LIMIT ' + (req.query.page - 1) * req.query.size + ',' + req.query.page * req.query.size
    // let ordersq3=`SELECT *,a.num w_order_goods_center_num,a.id w_order_goods_center_id,b.id w_orderr_id FROM order_goods_center a LEFT JOIN orderr b ON a.order_id=b.id LEFT JOIN goods c ON a.goods_id=c.id WHERE  a.order_id IN (${arr2})`
    db.query(ordersql, (err, data) => {
        // console.log(data)
        for(let v of data){
            arr.push(v.order_id);
            arr2=arr.toString()
           
        }
        let ordersq3=`SELECT *,a.num w_order_goods_center_num,a.id w_order_goods_center_id,b.id w_orderr_id FROM order_goods_center a LEFT JOIN orderr b ON a.order_id=b.id LEFT JOIN goods c ON a.goods_id=c.id WHERE  a.order_id IN (${arr2})`
        // console.log(arr2)
        if (data) {
            db.query(ordersql2, (err, data1) => {
                // console.log(data1)
                if (data1) {
                    db.query(ordersq3, (err, data2) => {
                        console.log(data2)
                        if (data1) {
                            res.json({
                                code: 200,
                                msg: '订单查询成功',
                                data: {
                                    count: data1[0].count,
                                    data: data,
                                    data2:data2
                                }
        
                            })
                        }
                    })
                }
            })
        } else {
            res.json({
                code: 500,
                msg: '订单查询失败'
            })
        }
    })
})

// 条件查询
apps.get('/order/mohuchax', function (req, res) {
    let arr=[]
    let reqs = req.query;
    let time = '';
    switch (reqs.timeliexin) {
        case '创建时间':
            time = 'a.create_time'
            break;
        case '支付时间':
            time = 'a.pay_time'
            break;
        case '发货时间':
            time = 'a.send_goods_time'
            break;
        case '完成时间':
            time = 'a.order_end_time'
            break;
        case '':
            time = 'a.create_time'
            break;
    }
    let tel = ''
    switch (reqs.weixinnicheng) {
        case '':
            tel = 'c.name'
            break;
        case '店铺名称':
            tel = 'c.name'
            break;
        case '收获人名称':
            tel = 'b.user_name'
            break;
        case '收货人手机号':
            tel = 'b.tel'
            break;
    }
    if (reqs.timeinputkai != '' || reqs.timeinputjie != '') {
        let sql = 'SELECT *,c.name store_name,a.id order_id FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 and a.express_no like "%' + reqs.bianhaoshuju + '%" and d.name like "%' + reqs.shangpin + '%" and ' + tel + ' like "%' + reqs.weixininput + '%" AND a.state like "%' + reqs.orderstatus + '%" and a.type like "%' + reqs.jiestatus + '%" and ' + time + ' BETWEEN ' + reqs.timeinputkai + ' and ' + reqs.timeinputjie + ' LIMIT ' + (reqs.page - 1) * reqs.size + ',' + reqs.page * reqs.size + '';
        let sql2 = 'SELECT count(*) count FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 and a.express_no like "%' + reqs.bianhaoshuju + '%" and d.name like "%' + reqs.shangpin + '%" and ' + tel + ' like "%' + reqs.weixininput + '%" AND a.state like "%' + reqs.orderstatus + '%" and a.type like "%' + reqs.jiestatus + '%" and ' + time + ' BETWEEN ' + reqs.timeinputkai + ' and ' + reqs.timeinputjie + ''
        db.query(sql, function (err, data) {
            for(let v of data){
                arr.push(v.order_id);
                arr2=arr.toString()
               
            }
            let ordersq3=`SELECT *,a.num w_order_goods_center_num,a.id w_order_goods_center_id,b.id w_orderr_id FROM order_goods_center a LEFT JOIN orderr b ON a.order_id=b.id LEFT JOIN goods c ON a.goods_id=c.id WHERE  a.order_id IN (${arr2})`
            if (data) {
                db.query(sql2, function (err, data1) {
                    db.query(ordersq3, (err, data2) => {
                        console.log(data2)
                        if (data2){
                            res.json({
                                code: 200,
                                msg: '订单查询成功',
                                data: {
                                    count: data1[0].count,
                                    data: data,
                                    data2:data2
                                }
        
                            })
                        }
                    })
                })
            } else {
                res.json({
                    code: 500,
                    msg: '订单失败'
                })
            }
        })
    } else if (reqs.timeinputkai == '' || reqs.timeinputjie == '') {
        let sql = 'SELECT *,c.name store_name,a.id order_id FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 and a.express_no like "%' + reqs.bianhaoshuju + '%" and d.name like "%' + reqs.shangpin + '%" and ' + tel + ' like "%' + reqs.weixininput + '%" AND a.state like "%' + reqs.orderstatus + '%" and a.type like "%' + reqs.jiestatus + '%"  LIMIT ' + (reqs.page - 1) * reqs.size + ',' + reqs.page * reqs.size + '';
        let sql2 = 'SELECT count(*) count FROM  orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 and a.express_no like "%' + reqs.bianhaoshuju + '%" and d.name like "%' + reqs.shangpin + '%" and ' + tel + ' like "%' + reqs.weixininput + '%" AND a.state like "%' + reqs.orderstatus + '%" and a.type like "%' + reqs.jiestatus + '%"'
        db.query(sql, function (err, data) {
            for(let v of data){
                arr.push(v.order_id);
                arr2=arr.toString()
               
            }
            let ordersq3=`SELECT *,a.num w_order_goods_center_num,a.id w_order_goods_center_id,b.id w_orderr_id FROM order_goods_center a LEFT JOIN orderr b ON a.order_id=b.id LEFT JOIN goods c ON a.goods_id=c.id WHERE  a.order_id IN (${arr2})`
            if (data) {
                db.query(sql2, function (err, data1) {
                    db.query(ordersq3, (err, data2) => {
                        console.log(data2)
                        if (data2){
                            res.json({
                                code: 200,
                                msg: '订单查询成功',
                                data: {
                                    count: data1[0].count,
                                    data: data,
                                    data2:data2
                                }
        
                            })
                        }
                    })
                })
            } else {
                res.json({
                    code: 500,
                    msg: '订单失败'
                })
            }
        })
    }
})
//订单回显接口
apps.get('/order/orderid',function(req,res){
    let ordersql='SELECT *,c.name store_name,a.id order_id FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id  where a.id='+req.query.id+'';
    let ordersql2='select * from order_goods_center where order_id=' +req.query.id+'';
    db.query(ordersql,(err,data)=>{
        if(data){
           db.query(ordersql2,(err,data1)=>{
               if(data1){
                res.json({
                    code:200,
                    msg:'查询成功',
                    data:{
                        data,
                        data1
                    }
                })
               }
           })
        }else{
            res.json({
                code:500,
                msg:'查询失败'
            })
        }
    })
})
// 商品回显
apps.get('/ordergoodsid',function(req,res){
    
    let ordersql=`SELECT *,b.num order_goods_num FROM order_goods_center b  LEFT JOIN goods a ON b.goods_id=a.id WHERE   b.order_id=${req.query.id} AND a.id IN (${req.query.ids})`
    db.query(ordersql,(err,data)=>{
        if(data){
            res.json({
                code:200,
                msg:'查询成功',
                data:data
            })
        }
    })
})
module.exports = apps;